package vtt.android.docbaovn.fragments;

import vtt.android.library.utils.VttGA;
import vtt.android.library.utils.VttUtils;
import vtt.android.docbaovn.R;
import vtt.android.docbaovn.dialogs.DialogManager;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.RenderPriority;
import fr.castorflex.android.smoothprogressbar.SmoothProgressBar;

public abstract class BaseWebFragment extends BaseFragment {

    // main webview
    protected WebView mWebView;

    // progress bar
    protected SmoothProgressBar mProgressBar;

    /**
     * parent activity
     */
    protected Activity mParentAct;

    protected int mPageIndex;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mParentAct = activity;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.main, menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_refresh:
            log.d("VTT>>> action_refresh" + "");
            if (!VttUtils.checkNetworkConnection(mParentAct)) {
                mDialogManager.showCommonDialog(DialogManager.DIALOG_TYPE_INFOR, getString(R.string.msg_title_notice),
                        getString(R.string.msg_network_error), null);
                return true;
            }
            
            reloadPage();
            
            return true;
        case R.id.action_menu_rate_app:
            VttGA.sendEvent(mParentAct, "BaseWebFragment", "Gop Y", "", 0);
            VttUtils.openGooglePlayMarket(mParentAct, mParentAct.getPackageName());
            break;
        default:
            break;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * reload page
     */
    protected void reloadPage() {
        if (mWebView != null) {
            mWebView.reload();
        }
    }

    /**
     * initiate common setting webview
     */
    protected void initCommonWebview() {

        mWebView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
        mWebView.setScrollContainer(true);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        // webSettings.setPluginState(PluginState.ON);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        //webSettings.setRenderPriority(RenderPriority.HIGH);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

        mWebView.setWebChromeClient(MyWebChromeClient());
    }

    /**
     * webchrome client
     * 
     * @return WebChromeClient
     */
    protected WebChromeClient MyWebChromeClient() {
        return new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (progress >= 100) {
                    mProgressBar.progressiveStop();
                }
            }
        };
    }

    /**
     * check webview can go back
     */
    public void goBackWebView() {
        if (mWebView != null && mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            mParentAct.finish();
        }
    }

    @Override
    public void onDestroy() {
        if (mWebView != null) {
            mWebView.stopLoading();
            mWebView.setVisibility(View.GONE);
            ViewGroup parent = (ViewGroup) mWebView.getParent();
            if (parent != null) {
                parent.removeView(mWebView);
            }
            mWebView.removeAllViews();
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }
}
